package pvt.test.democountryapdater;

import java.util.ArrayList;
import java.util.Locale;

import android.R.anim;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class CountryAdapter extends ArrayAdapter<Country> {
	private Context context;
	private int resID;
	private ArrayList<Country> arrayCountry;
	private ArrayList<Country> originCountry;
	private View view;
	int count = 0;
	private MainActivity mainActivity;
	private String mSearchText = "";

	@Override
	public Filter getFilter() {
		// TODO Auto-generated method stub
		return new Filter() {

			@Override
			protected void publishResults(CharSequence constraint,
					FilterResults results) {
				// TODO Auto-generated method stub

				arrayCountry.clear();
				ArrayList<Country> listCountries = (ArrayList<Country>) results.values;
				for (int i = 0; i < listCountries.size(); i++) {
					Country country = listCountries.get(i);
					arrayCountry.add(country);
				}
				notifyDataSetChanged();

			}

			@Override
			protected FilterResults performFiltering(CharSequence constraint) {
				// TODO Auto-generated method stub
				FilterResults filterResults = new FilterResults();
				String pTextSearch = constraint.toString().toLowerCase(
						Locale.ENGLISH);
				mSearchText = pTextSearch;
				ArrayList<Country> countries = new ArrayList<Country>();

				for (Country country : originCountry) {
					if (country.getmNameEN().toLowerCase(Locale.ENGLISH)
							.contains(pTextSearch)) {
						countries.add(country);
					}
				}

				filterResults.values = countries;
				filterResults.count = countries.size();

				return filterResults;
			}
		};
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (countIsChecked() == 0 && mainActivity.actionMode != null) {
			mainActivity.actionMode.finish();
			mainActivity.actionMode = null;

		}
		return arrayCountry.size();
	}

	public int countIsChecked() {
		int count = 0;

		for (Country pCountry : this.arrayCountry) {
			if (pCountry.isChecked() == true) {
				count++;
			}

		}

		return count;

	}

	public CountryAdapter(Context context, int resource,
			ArrayList<Country> objects) {
		super(context, resource, objects);
		this.context = context;
		this.resID = resource;
		this.arrayCountry = objects;
		this.originCountry = new ArrayList<Country>(arrayCountry);
		mainActivity = (MainActivity) context;

	}

	private class ViewHolder {
		ImageView imageView;
		TextView txtNameEN;
		TextView txtNameVI;

	}

	@SuppressLint("NewApi")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Log.i("PVT", "isScroll");
		view = convertView;
		ViewHolder holder;
		if (view == null) {
			holder = new ViewHolder();
			LayoutInflater layoutInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = layoutInflater.inflate(resID, null);
			holder.imageView = (ImageView) view.findViewById(R.id.imageCountry);
			holder.txtNameEN = (TextView) view.findViewById(R.id.txtNameEn);
			holder.txtNameVI = (TextView) view.findViewById(R.id.txtNameVi);
			view.setTag(holder);

		} else {
			holder = (ViewHolder) view.getTag();
		}

		final Country country = arrayCountry.get(position);
		holder.imageView.setImageDrawable(country.getmImageCountry());
		if (!mSearchText.isEmpty()) {

			int startIndex = country.getmNameEN().toLowerCase(Locale.ENGLISH)
					.indexOf(mSearchText);
			int endIndex = startIndex + mSearchText.length();
			SpannableString spannableString = new SpannableString(
					country.getmNameEN());
			spannableString.setSpan(new BackgroundColorSpan(Color.CYAN),
					startIndex, endIndex,
					SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
			holder.txtNameEN.setText(spannableString);

		} else {
			holder.txtNameEN.setText(country.getmNameEN());

		}
		holder.txtNameVI.setText(country.getmNameVI());
		CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox1);
		checkBox.setChecked(false);
		checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {

					country.setChecked(true);
					// start ActionMode;
					if (mainActivity.actionMode == null) {
						mainActivity.actionMode = mainActivity
								.startActionMode(mainActivity.callback);

					}

				} else {

					country.setChecked(false);

					if (mainActivity.actionMode != null
							&& countIsChecked() == 0) {
						// Huy actionmode
						mainActivity.actionMode.finish();
						mainActivity.actionMode = null;
					}
				}

			}
		});

		/*
		 * LayoutInflater layoutInflater = (LayoutInflater) context
		 * .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view =
		 * layoutInflater.inflate(resID, null); ImageView imageView =
		 * (ImageView) view.findViewById(R.id.imageCountry); TextView textView =
		 * (TextView) view.findViewById(R.id.txtNameEn); TextView textView2 =
		 * (TextView) view.findViewById(R.id.txtNameVi); Country country =
		 * arrayCountry.get(position);
		 * imageView.setImageDrawable(country.getmImageCountry());
		 * textView.setText(country.getmNameEN());
		 * textView2.setText(country.getmNameVI());
		 */
		return view;
	}
}
